import errorcodeJSON from '@/assets/json/errorcode'
import { handleLogout } from '@/views/login/service'

export default {
  install(Vue) {
    const message = Vue.prototype.$Message
    // http请求统一拦截处理
    Vue.http.interceptors.push(function (request, next) {
      request.credentials = true
      if (request && request.params) {
        // 请求前处理增加时间标记缓存...
        request.params['_'] = new Date().getTime()
      }
      next(function (response) {
        // 请求结果拦截处理
        let sts = response.status
        if (sts >= 500) {
          switch (sts) {
            case 500:
              message.error('服务可能正在升级维护，请稍后重试!')
              break
            case 501:
              message.error('服务器繁忙，请稍后重试!')
              break
            case 502:
              message.error('服务可能正在升级维护，请稍后重试!')
              break
            case 503:
              // window.location.href = "https://36.112.31.251:3443/yunwei";
              break
            case 504:
              message.error('服务器繁忙，请稍后重试!')
              break
            case 505:
              message.error('服务可能正在升级维护，请稍后重试!')
              break
          }
          return
        }

        let status = response.body && response.body.status
        const path = window.location.pathname
        const errorMessage = (response.body && response.body.message) || '错误'
        if (status == errorcodeJSON.login) {
          if (path !== '/login') {
            message.error(errorMessage)
            // message.error('TTTTTTTTTTTTTT')
            /* localStorage.removeItem("depCode");
            localStorage.removeItem("depId");
            localStorage.removeItem("orgName");
            localStorage.removeItem("organId");
            localStorage.removeItem("parentOrganId");
            localStorage.removeItem("preCfgGroupId");
            localStorage.removeItem("resource");
            localStorage.removeItem("roleType");
            localStorage.removeItem("userAccount");
            localStorage.removeItem("userInfo");
            localStorage.removeItem("userName"); */
            handleLogout()
          }
        } else if (status == errorcodeJSON.previlige) {
          message.error(errorMessage || '错误')
        }
      })
    })

    Vue.prototype.hasPermission = function (target, resourceArray) {
      /* if (gl.develop) {
        return true
      } */
      // 返回处理后的值
      let isHavePermission = false

      if (!resourceArray) {
        let resources = sessionStorage.getItem('resource')
        resourceArray = JSON.parse(resources)
      }

      if (resourceArray) {
        for (let urlObj in resourceArray) {
          let resourceExists = resourceArray[urlObj]
          if (resourceExists) {
            if (resourceExists.url == target) {
              isHavePermission = true
            } else if (resourceExists.children) {
              isHavePermission = this.hasPermission(target, resourceExists.children)
            }
            if (isHavePermission) {
              return true
            }
          }
        }
      }
      return false
    }

    // Vue.prototype.getRequest= function (url,cb) {
    //   return new Promise((resolve, reject) => {
    //         Vue.http.get(
    //           url,
    //           {emulateJSON: true}
    //         ).then(cb=>{
    //         })
    //         .catch((err) => {
    //           console.log(err)
    //           resolve(err);
    //         });
    //     })
    //   }
  },
}
